package com.zjlanyun.erp.approve;

import java.util.ArrayList;

import com.astuetz.PagerSlidingTabStrip;
import com.zjlanyun.erp.Common;
import com.zjlanyun.erp.R;
import com.zjlanyun.erp.adapter.AbFragmentPagerAdapter;
import com.zjlanyun.erp.utils.Toastor;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ApproveFrame extends FragmentActivity {

	private ViewPager viewPager;
	private PagerSlidingTabStrip pagerSlidingTabStrip;
	private ArrayList<Fragment> pages;
	private String[] titles = { "明细", "主单据", "审批历史" };
	private Context mContext = this;
	private TextView title;
	private LinearLayout ll_back;
	private Button btn_approve;
	private Toastor toastor;
	private String trantype;
	
	private LinearLayout ll_agree_wrap, agree, reject_higher, reject_originator;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.approve_frame);

		initView();
		initViewPager();
	}

	// 初始化View
	private void initView() {
		title = (TextView) findViewById(R.id.title);
		btn_approve = (Button) findViewById(R.id.btn_approve);
		ll_back = (LinearLayout) findViewById(R.id.ll_back);
		viewPager = (ViewPager) findViewById(R.id.view_pager);
		pagerSlidingTabStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
		ll_agree_wrap = (LinearLayout) findViewById(R.id.ll_agree_wrap);
		agree = (LinearLayout) findViewById(R.id.agree);
		reject_higher = (LinearLayout) findViewById(R.id.reject_higher);
		reject_originator = (LinearLayout) findViewById(R.id.reject_originator);
		
		
		pages = new ArrayList<Fragment>();
		pages.add(new ApproveDetail());
		pages.add(new ApproveMain());
		pages.add(new ApproveRecord());

		Intent intent = getIntent();
		trantype = intent.getStringExtra("trantype");
		title.setText(Common.getApproveType().get(trantype) + "详情");
		
		toastor = new Toastor();
		
		if(trantype.equals("21")){
			ll_agree_wrap.setVisibility(View.VISIBLE);
			btn_approve.setVisibility(View.GONE);
		}else{
			ll_agree_wrap.setVisibility(View.GONE);
			btn_approve.setVisibility(View.VISIBLE);
		}
		
		//后退
		ll_back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		
		//审核
		btn_approve.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				AlertDialog.Builder builder = new Builder(mContext);
		    	builder.setMessage("审批操作执行后不能修改，确定要审批吗？");
		    	builder.setTitle("提示");
		    	builder.setPositiveButton("确定", new OnClickListener() {
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						btn_approve.setVisibility(View.GONE);
						toastor.showShortToast("审批成功");
					}
				});
		    	builder.setNegativeButton("取消", new OnClickListener() {
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
					}
				});
		    	builder.create().show();
			}
		});
		
		//同意
		agree.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(mContext, ApproveForm.class);
				intent.putExtra("trantype", trantype);
				intent.putExtra("approveType", 1);
				startActivityForResult(intent, 1);
			}
		});
		
		//驳回上级
		reject_higher.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(mContext, ApproveForm.class);
				intent.putExtra("trantype", trantype);
				intent.putExtra("approveType", 2);
				startActivityForResult(intent, 1);
			}
		});
		
		//驳回制单人
		reject_originator.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(mContext, ApproveForm.class);
				intent.putExtra("trantype", trantype);
				intent.putExtra("approveType", 3);
				startActivityForResult(intent, 1);
			}
		});
	}
	

	// 初始化viewpager
	private void initViewPager() {
		// 初始化viewpager
		AbFragmentPagerAdapter fp = new AbFragmentPagerAdapter(
				getSupportFragmentManager(), pages, titles);
		viewPager.setAdapter(fp);
		viewPager.setOffscreenPageLimit(3);
		
		pagerSlidingTabStrip.setDividerColorResource(R.color.color_gray_deep);
		pagerSlidingTabStrip.setBackgroundColor(Color.WHITE);
		// 指示器颜色
		pagerSlidingTabStrip.setIndicatorColorResource(R.color.color_gray_deep);
		// 指示器的高度
		pagerSlidingTabStrip.setIndicatorHeight(Common.dp2px(2, mContext));
		// 字体大小
		pagerSlidingTabStrip.setTextSize(Common.dp2px(16, mContext));
		// 设置选中颜色
		pagerSlidingTabStrip.setShouldExpand(true);
		pagerSlidingTabStrip.setViewPager(viewPager);
		viewPager.setCurrentItem(1);
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		if(arg0 == 1 && arg1 == 1){
			if(arg2.getBooleanExtra("result", false)){
				toastor.showShortToast("提交成功");
			}else{
				toastor.showShortToast("提交失败");
			}
		}
	}
}
